//
//  HMHomeTitleButton.swift
//  HMWeibo12
//
//  Created by iOS on 16/9/20.
//  Copyright © 2016年 iOS. All rights reserved.
//

import UIKit

class HMHomeTitleButton: UIButton {
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init(name: String?) {
        // 创建按钮
        super.init(frame: CGRect())
        
        // 设置按钮图片
        self.setImage(UIImage(named: "navigationbar_arrow_down"), for: .normal)
        self.setTitle(name, for: .normal)
        self.setTitleColor(UIColor.black, for: .normal)
        
        self.sizeToFit()
    }

    // 重新设置按钮内容的frame,有2种方案
//    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
//        <#code#>
//    }
//    
//    override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
//        
//    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 将文字的origin.x 设置为0
        self.titleLabel?.frame.origin.x = 0
        
        // 将图片放到文字后面
        self.imageView?.frame.origin.x = (self.titleLabel?.frame.width ?? 0) + 3
    }
}
